import { getPanelList } from "../api/panel";
const panel = {
    namespaced: true,
    state: {
        names: [],
        datas: [],
        series: [],
        dateObj: {},
        dts: [],
        colorArr: ["#3888fa", "#FF005A", "#ccc", "aqua"],
    },

    mutations: {
        setPanelList(state, data) {
            state.names = []
            state.datas = []
            state.series = []
            state.dts = []
            state.dateObj = {}
            console.log(data);

            if (!data.length) {
                return
            }
            let str = data[0].name
            let num = 0
            data.forEach(val => {
                //x轴
                if (state.datas.indexOf(val.date) == -1) {
                    state.datas.push(val.date)
                    state.datas = state.datas.sort()
                }
                //小图标
                if (state.names.indexOf(val.name) == -1) {
                    state.names.push(val.name)
                }
                //{人名：时间[]}
                if (state.dateObj[val.name]) {
                    state.dateObj[val.name].push(val.date)
                } else {
                    state.dateObj[val.name] = []
                    state.dateObj[val.name].push(val.date)
                }
                if (val.name == str) {
                    if (!state.series[num]) {
                        state.series[num] = {}
                        state.series[num].name = val.name
                        state.series[num].type = "line"
                        state.series[num].itemStyle = {
                            color: state.colorArr[num],
                        };
                    }
                } else {
                    str = val.name
                    num++
                    state.series[num] = {}
                    state.series[num].name = val.name
                    state.series[num].type = "line"
                    state.series[num].itemStyle = {
                        color: state.colorArr[num],
                    };
                }
                state.series[num].data = []
            })
            state.series.forEach((val, ind) => {
                state.datas.forEach((value, index) => {
                    // console.log(value);
                    if (state.dateObj[val.name].indexOf(value) != -1) {
                        let res = data.find(item => {
                            return item.name == val.name && item.date == value
                        })
                        // console.log(res.score);
                        state.series[ind].data[index] = res.score
                    } else {
                        state.series[ind].data[index] = 0
                    }
                })
            })
            // console.log(state.datas);

            state.datas = state.datas.sort()
            // console.log(state.datas);

            state.datas.forEach(res => {
                // console.log(res);
                state.dts.push(res.slice(8, 11))
            })
        }
    },
    actions: {
        async getPanel({ commit }, date) {
            let res = await getPanelList(date)
            // console.log(res);
            commit('setPanelList', res.data.data)
            return res.data.data
        }
    },
}
export default panel